<template>
  <div>
    <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span>分组管理</span>
      <el-button style="float: right" type="primary" @click="addDialogVisible = true" plain >添加分组</el-button>
    </div>
          <!-- 搜索和添加组件区域
            @clear='getUsersList'
            @input='getUsersList'
            -->
      <el-row :gutter='20'>
        <el-col :span='7'>
          <el-input
            placeholder='请输入内容'
            v-model='searchName'
            class='input-with-select'
            clearable
          >
            <el-button
              slot='append'
              icon='el-icon-search'
              @click='getroleList'
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
    <div>
      <el-table :data="roleList" border style="width: 100%">
        <el-table-column prop="grpId" label="分组编号">
        </el-table-column>
        <el-table-column prop="grpName" label="分组名称">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
             <el-tooltip
              class='item'
              effect='dark'
              content='修改信息'
              placement='top'
            >
             <el-button
             size="mini"
              type="info"
              icon="el-icon-message"
              @click='getone(scope.row.grpId)'
              circle>
            </el-button>
            </el-tooltip>
            <el-tooltip
              class='item'
              effect='dark'
              content='修改权限'
              placement='top'
            >
            <el-button
            size="mini"
              type="primary"
              icon="el-icon-edit"
              circle
              @click="getoneMenus(scope.row.grpId, scope.row.grpName)"
            ></el-button>
             </el-tooltip>
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              circle
              @click="del(scope.row.grpId)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="rows"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
    </el-card>
<!--    修改分组-->
    <el-dialog
      title="编辑分组"
      :visible.sync="dispatcherDialogVisible"
      width="50%"
      @close="cleareditForm">
      <!-- 对话框主体内容 -->
      <el-form ref="editFrom" :model="editFrom" :rules="editFormRules" label-width="80px">
        <el-form-item label="分组编号" prop="grpId" >
          <el-input v-model="editFrom.grpId" disabled ></el-input>
        </el-form-item>
        <el-form-item label="分组姓名" prop="grpName"  >
          <el-input v-model="editFrom.grpName"  ></el-input>
        </el-form-item>
      </el-form>
      <!-- 对话框的操作按钮 -->
      <span slot="footer" class="dialog-footer">
            <el-button @click="dispatcherDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="edit">确 定</el-button>
          </span>
    </el-dialog>
    <!--    添加分组-->
    <el-dialog
      title="添加分组"
      :visible.sync="addDialogVisible"
      width="50%"
      @close="clearaddForm">
      <!-- 对话框主体内容 -->
      <el-form ref="addFrom" :model="addFrom" :rules="addFormRules" label-width="80px">
        <el-form-item label="分组姓名" prop="grpName"  >
          <el-input v-model="addFrom.grpName"  ></el-input>
        </el-form-item>
      </el-form>
      <!-- 对话框的操作按钮 -->
      <span slot="footer" class="dialog-footer">
            <el-button @click="addDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="add">确 定</el-button>
          </span>
    </el-dialog>

    <!-- 修改分组对应菜单-->
    <el-dialog
      title="编辑权限"
      :visible.sync="dispatcherDialogVisibleMenus"
      width="50%"
      @close="clearPreForm">
      <!-- 对话框主体内容 -->
      <el-form ref="editFrom" :model="editFromMenus" :rules="editFormRules" label-width="80px">
        <el-form-item label="分组姓名"  >
          <el-input v-model="editFromMenus.grpName" disabled ></el-input>
        </el-form-item>
        <el-form-item>
          <template>
              <el-tree
              :data="menusData"
              show-checkbox
              :default-checked-keys="checkedCities"
              node-key="rightCode"
              ref="tree"
              highlight-current
              :props="defaultProps">
            </el-tree>
          </template>
        </el-form-item>
      </el-form>
      <!-- 对话框的操作按钮 -->
      <span slot="footer" class="dialog-footer">
            <el-button @click="dispatcherDialogVisibleMenus = false">取 消</el-button>
            <el-button type="primary" @click="updateMenus">确 定</el-button>
          </span>
    </el-dialog>
    </div>
</template>

<script>
export default ({
  data () {
    return {
      menusData: [],
      defaultProps: {
        children: 'menus',
        label: 'rightText'
      },
      roleList: [],
      page: 1,
      rows: 5,
      total: 0,
      searchName: '',
      Rtable: {
        grpId: '',
        mentId: ''
      },
      Rptable: [],
      dispatcherDialogVisible: false,
      addDialogVisible: false,
      dispatcherDialogVisibleMenus: false,
      checkedCities: [],
      checkAll: false,
      isIndeterminate: true,
      results: [],
      editFrom: {
        grpId: '',
        grpName: ''
        // roleFlag: '',
      },
      editFromMenus: {
        grpId: '',
        grpName: ''
      },
      addFrom: {
        grpName: ''
      },
      editFormRules: {
        grpName: [
          { required: true, message: '名称不能为空', trigger: 'blur' },
          { min: 2, max: 50, message: '长度在2-15之间', trigger: 'blur' }
        ]
      },
      addFormRules: {
        grpName: [
          { required: true, message: '名称不能为空', trigger: 'blur' },
          { min: 2, max: 50, message: '长度在2-15之间', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async getroleList () {
      const param = {
        page: this.page,
        rows: this.rows,
        groupName: this.searchName
      }
      const { data: result } = await this.$http.post(
        'user-server/sys-group/page',
        this.$qs.stringify(param)
      )
      console.log(result.result)
      if (result.result === 'SUCCESS') {
        this.roleList = result.data.records
        this.total = result.data.total
      } else {
        this.$message.error('查询失败')
      }
    },
    handleSizeChange (size) {
      this.rows = size
      this.getroleList()
    },
    handleCurrentChange (page) {
      this.page = page
      this.getroleList()
    },
    // 加载菜单
    async getoneMenus (id, grpName) {
      this.editFromMenus.grpId = id
      this.editFromMenus.grpName = grpName
      const { data: result } = await this.$http.get('user-server/right/permis')

      // 加载所有的菜单
      this.menusData = result.data

      // 设置选中菜单
      const { data: results } = await this.$http.get('user-server/sys-grp-per/getByGrpId/' + id)
      this.checkedCities.length = 0
      results.data.forEach(element => {
        this.checkedCities.push(element.gfPerId)
      })
      this.dispatcherDialogVisibleMenus = true
    },
    async edit () {
      this.$refs.editFrom.validate(async valid => {
        if (!valid) {
          return
        }
        const { data: resule } = await this.$http.post('user-server/sys-group/update', this.editFrom)
        if (resule.result !== 'SUCCESS') {
          this.$message.error(resule.message)
        }
        this.$message.success('更新成功')
        this.getroleList()
        this.dispatcherDialogVisible = false
      })
    },
    // 显示修改个人信息
    async getone (id) {
      // 查询单个数据详情
      const { data: resule } = await this.$http.get('user-server/sys-group/selectById/' + id)
      this.editFrom = resule.data
      this.dispatcherDialogVisible = true
    },
    add () {
      this.$refs.addFrom.validate(async valid => {
        if (!valid) {
          return
        }
        const { data: resule } = await this.$http.post('user-server/sys-group/add', this.addFrom)
        if (resule.result !== 'SUCCESS') {
          this.$message.error(resule.message)
        } else {
          this.$message.success('添加成功')
        }
        this.getroleList()
        this.addDialogVisible = false
      })
    },
    cleareditForm () {
      this.$refs.editFrom.resetFields()
      this.checkedCities = []
    },
    clearaddForm () {
      this.$refs.addFrom.resetFields()
    },
    clearPreForm () {
      this.$refs.tree.setCheckedKeys([])
    },
    del (id) {
      this.$confirm('是否删除该分组?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: result } = await this.$http.delete('user-server/sys-group/delete/' + id)
        if (result.result !== 'SUCCESS') {
          this.$message({
            type: 'error',
            message: result.message
          })
        } else {
          this.$message({
            type: 'success',
            message: '删除成功'
          })
          this.getroleList()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    async updateMenus () {
      const { data: resule } = await this.$http.put('user-server/sys-grp-per/ids/' + this.editFromMenus.grpId, this.$refs.tree.getCheckedNodes())
      if (resule.result === 'SUCCESS') {
        this.$message.success('修改成功！')
      } else {
        this.$message.success(resule.message)
      }
      this.checkedCities.length = 0
      this.dispatcherDialogVisibleMenus = false
    }
  },
  created () {
    this.getroleList()
    // console.log(this.roleAvailable)
  }
})
</script>
